var mobileDateSchedule = {
	'selYear': '', // 选择的年份
	'selMonth': '', // 选择的月份
	'selDay': '', // 选择的日期
	'selTime': '', //选择的时间段
	'selData': '', //按月
	'selData1': '', //按天-开始
	'selData2': '', //按天-结束
	dateWrap: function(type) {
		var typeStr = ""
		if (type == "time") {
			typeStr = '<div class="sel-box"><div class="bg-4 choice-time"> <div class="choice-time1">选择日期<i></i></div> <div class="choice-time2">/</div> <div class="choice-time3">选择时段</div> </div></div>';
		} else if (type == "day") {
            typeStr = '<div class="sel-box"><div class="bg-4 choice-time"> <div class="choice-time1">开始日期<i></i></div> <div class="choice-time2">/</div> <div class="choice-time3">结束日期</div> </div></div>';
		} else if (type == "month") {
            typeStr = '<div class="sel-box"><div class="bg-4 choice-time"> <div class="choice-time1">开始日期<i></i></div> <div class="choice-time2">起</div> <div class="choice-time3">期限</div> </div></div>';
		}
		var dateHtml = '<div class="data-wrap serviceData">' +
			'<div class="date-header">' +
			'<a class="date-confirm">确认</a>' +
			'<a class="cont">取消</a>' +
			'</div>' +
			'<div>' +
			'<div class="year-box">' +
            typeStr +
			'<div class="show-list">' +
			'<div class="year-list"></div>' +
			'</div>' +
			'</div>' +
			'<div class="month-box">' +
			// '<div class="sel-box removeAllSelect">清空选择</div>' +
			'<div class="sel-box"><button type="button" class="removeAllSelect">清空选择</button></div>' +
			'<div class="show-list">' +
			'<div class="month-head">' +
			'<table>' +
			'<thead>' +
			'<td>日</td>' +
			'<td>一</td>' +
			'<td>二</td>' +
			'<td>三</td>' +
			'<td>四</td>' +
			'<td>五</td>' +
			'<td>六</td>' +
			'</thead>' +
			'</table>' +
			'</div>' +
			'<div class="month-list">' +
			'</div>' +
			'</div>' +
			'</div>' +
			'<div class="day-box">' +
			'<div class="show-list">' +
			'<div class="day-list">' +
			'</div>' +
			'</div>' +
			'</div>' +
			'</div>' +
			'</div>'
		return dateHtml
	},
	yearShow: function(min, max) { // 年列表展示
		var newYear = new Date().getFullYear();
        console.log(newYear)
        mobileDateSchedule.selYear = newYear;
	},
	getYear: function() { // 生成年份
		var e = window.event,
			elementDoc = e.srcElement || e.target,
			val = elementDoc.innerHTML
		// $('.year-box .sel-box').html(val + '年').show()
		$('.year-box .sel-box').html('<div class="bg-4 choice-time"> <div class="choice-time1">选择日期<i></i></div> <div class="choice-time2">/</div> <div class="choice-time3">选择时段<i></i></div> </div>').show();
		mobileDateSchedule.selYear = val
	},
	getDay: function(type, fullDate, isOrNot) { //生成日期
		var e = window.event,
			elementDoc = e.srcElement || e.target,
			eTable = $(elementDoc).parents('table').find('thead td').data('month'),
			val = $(elementDoc).data('date')

		if (type == "time") {
           	eTable = (eTable == 0?12:eTable)
            $('.year-box .sel-box .choice-time1').html(eTable + '月' + val + '日').show();
		} else if(type == "day") {
			var dateArr = fullDate.split("*");
            if ($('.choice-time i').closest("div").attr("class") == "choice-time1") {
                setOverDateChoseWhenStarChose( dateArr[0] + "-"+ dateArr[1] + "-" + dateArr[2]);
                mobileDateSchedule.selData1 = dateArr.join("-");

            }
			// 按天排期判断其他不影响
            if(!isOrNot) {
                $('.choice-time i').closest("div").attr("dateattr", dateArr[0] + "-"+ dateArr[1] + "-" + dateArr[2]);
                $('.choice-time i').closest("div").html(dateArr[1] + '月' + dateArr[2] + '日').show();
            }

            if($(".choice-time1").attr('dateattr') != dateArr[0] + "-"+ dateArr[1] + "-" + dateArr[2]) {
                mobileDateSchedule.selData2 = dateArr.join("-");
			}

        } else if(type == "month") {
            var dateArr = fullDate.split("-");
            $('.year-box .sel-box .choice-time1').html(dateArr[1] + '月' + dateArr[2] + '日').show().attr("dateattr", fullDate);
        }
        $('.year-box .sel-box .choice-time3').html($('.year-box .sel-box .choice-time3').text()+'<i></i>');
		$('.month-box .sel-box').html('<button type="button" class="removeAllSelect">清空选择</button>').show();
		mobileDateSchedule.selMonth = eTable
	},
	monthShow: function(year) { //日期表格展示
		var html = '';
		var	newYear = new Date(year);
		  //今年从当前月份开始算
			var nowMonth = new Date().getMonth() + 1,

				nowDate = new Date().getDate();
			for(var i = nowMonth; i < (nowMonth+12); i++) {

				var daysnum = mobileDateSchedule.getAllday(new Date(year, i - 1, 1)),
					days = new Date(year, i - 1, 1).getDay(),
					tr = '<tr>',
					chinaDay = mobileDateSchedule.getChinaDay(i % 12);
				html += '<table>' +
					'<thead>' +
					'<td colspan="7" data-month="' + (i % 12) + '">' + chinaDay + '月</td>' +
					'</thead>';
				for(var k = 0; k < days; k++) {
					tr += '<td></td>';
				};
				for(var j = 1; j < daysnum + 1; j++) {
					if((j + days) % 7 != 0) {
						if(j < nowDate && i == nowMonth) {
							tr += '<td class="over"  data-date="' + j + '"><div>' + j +'</div><div class="isnull">满</div></td>';
						} else if(j == nowDate && i == nowMonth) {
							tr += '<td class="nows"  data-date="' + j + '"><div>今天</div><div class="isnull">满</div></td>';
						} else {
							tr += '<td  data-date="' + j + '">' + j + '<div class="isnull">满</div></td>';
						};
					} else {
						if(j < nowDate && i == nowMonth) {
							tr += '<td class="over"  data-date="' + j + '"><div>' + j +'</div><div class="isnull">满</div></td></tr><tr>';
						} else if(j == nowDate && i == nowMonth) {
							tr += '<td class="nows"  data-date="' + j + '"><div>今天</div><div class="isnull">满</div></td>';
						} else {
							tr += '<td  data-date="' + j + '"><div>' + j +'</div><div class="isnull">满</div></td></tr><tr>';
						}
					}
				}
				html += tr + '</tr></table>';
			}

		return html;

	},
    getSpecificDate: function (year, month, date) {
		month = month ==0 ?12:month;
        return year + "-"+ month + "-" + date;
    },
    monthShowforSchedule1: function(year) { //日期表格展示
        var html = '';
        var	newYear = new Date(year);
        //今年从当前月份开始算
        var nowMonth = new Date().getMonth() + 1,
            nowDate = new Date().getDate();
        for(var i = nowMonth; i < (nowMonth+12); i++) {

            var daysnum = mobileDateSchedule.getAllday(new Date(year, i - 1, 1)),//当前月份的总天数
				nowyear = new Date(year, i - 1, 1).getFullYear();
                days = new Date(year, i - 1, 1).getDay(),
                tr = '<tr>',
                chinaDay = mobileDateSchedule.getChinaDay(i % 12);
            html += '<table>' +
                '<thead>' +
                '<td colspan="7" data-month="' + (i % 12) + '">' + chinaDay + '月</td>' +
                '</thead>';
            for(var k = 0; k < days; k++) {
                tr += '<td></td>';
            };
            for(var j = 1; j < daysnum + 1; j++) {
                if((j + days) % 7 != 0) {
                    if(j < nowDate && i == nowMonth) {
                        tr += '<td class="over"  data-date="' + j + '" specilDate="'+mobileDateSchedule.getSpecificDate(nowyear,(i % 12),j)+'"><div data-date="' + j + '">' + j +'</div></td>';
                    } else if(j == nowDate && i == nowMonth) {
                        tr += '<td class="nows"  data-date="' + j + '" specilDate="'+mobileDateSchedule.getSpecificDate(nowyear,(i % 12),j)+'"><div data-date="' + j + '">今天</div></td>';
                    } else {
                        tr += '<td  data-date="' + j + '" specilDate="'+mobileDateSchedule.getSpecificDate(nowyear,(i % 12),j)+'"><div data-date="' + j + '">' + j +'</div></td>';


                    };
                } else {
                    if(j < nowDate && i == nowMonth) {
                        tr += '<td class="over"  data-date="' + j + '" specilDate="'+mobileDateSchedule.getSpecificDate(nowyear,(i % 12),j)+'"><div data-date="' + j + '">' + j +'</div></td></tr><tr>';
                    } else if(j == nowDate && i == nowMonth) {
                        tr += '<td class="nows"  data-date="' + j + '" specilDate="'+mobileDateSchedule.getSpecificDate(nowyear,(i % 12),j)+'"><div data-date="' + j + '">今天</div></td></tr><tr>';
                    } else {
                        tr += '<td  data-date="' + j + '" specilDate="'+mobileDateSchedule.getSpecificDate(nowyear,(i % 12),j)+'"><div data-date="' + j + '">' + j +'</div></td></tr><tr>';
                    }
                }


            }
            html += tr + '</tr></table>';
        }

        return html;

    },
    monthShowforSchedule2: function(year) { //日期表格展示
        var html = '';
        var	newYear = new Date(year);
        //今年从当前月份开始算
        var nowMonth = new Date().getMonth() + 1,
            nowDate = new Date().getDate();
        for(var i = nowMonth; i < (nowMonth+12); i++) {

            var daysnum = mobileDateSchedule.getAllday(new Date(year, i - 1, 1)),
                theyear = new Date(year, i - 1, 1).getFullYear(),
                days = new Date(year, i - 1, 1).getDay(),
                tr = '<tr>',
                chinaDay = mobileDateSchedule.getChinaDay(i % 12);
            html += '<table>' +
                '<thead>' +
                '<td colspan="7" data-month="' + (i % 12) + '">' + chinaDay + '月</td>' +
                '</thead>';
            for(var k = 0; k < days; k++) {
                tr += '<td></td>';
            };
            var themonth = i%12==0?12:(i % 12);
			themonth = themonth<10?"0"+themonth:themonth;
            for(var j = 1; j < daysnum + 1; j++) {
                var fullDate = theyear +"*"+themonth+"*"+ j;
                if((j + days) % 7 != 0) {
                    if(j < nowDate && i == nowMonth) {
                        tr += '<td class="over"  full-date="'+fullDate + '"><div>' + j +'</div></td>';
                    } else if(j == nowDate && i == nowMonth) {
                        tr += '<td class="nows"  full-date="'+fullDate+ '" data-date="' + j + '"><div>今天</div></td>';
                    } else {
                        tr += '<td  full-date="'+fullDate + '" data-date="' + j + '"><div>' + j +'</div></td>';
                    };
                } else {
                    if(j < nowDate && i == nowMonth) {
                        tr += '<td class="over"  full-date="'+fullDate + '" data-date="' + j + '"><div>' + j +'</div></td></tr><tr>';
                    } else if(j == nowDate && i == nowMonth) {
                        tr += '<td class="nows"  full-date="'+fullDate + '" data-date="' + j + '"><div>今天</div></td></tr><tr>';
                    } else {
                        tr += '<td  full-date="'+fullDate+'" data-date="' + j + '"><div>' + j +'</div></td></tr><tr>';
                    }
                }
            }
            html += tr + '</tr></table>';
        }

        return html;

    },

    monthShowforSchedule3: function(year) { //日期表格展示
        var html = '';
        var	newYear = new Date(year);
        //今年从当前月份开始算
        var nowMonth = new Date().getMonth() + 1,
            nowDate = new Date().getDate();
        for(var i = nowMonth; i < (nowMonth+12); i++) {

            var daysnum = mobileDateSchedule.getAllday(new Date(year, i - 1, 1)),
				theyear = new Date(year, i - 1, 1).getFullYear(),
                days = new Date(year, i - 1, 1).getDay(),
                tr = '<tr>',
                chinaDay = mobileDateSchedule.getChinaDay(i % 12);
            html += '<table>' +
                '<thead>' +
                '<td colspan="7" data-month="' + (i % 12) + '">' + chinaDay + '月</td>' +
                '</thead>';
            for(var k = 0; k < days; k++) {
                tr += '<td></td>';
            };
            var themonth = i%12==0?12:(i % 12);
            for(var j = 1; j < daysnum + 1; j++) {
                if((j + days) % 7 != 0) {
                    if(j < nowDate && i == nowMonth) {
                        tr += '<td class="over"  full-date="'+theyear +"-"+themonth+"-"+ j + '" data-date="' + j + '"><div>' + j + '</div></td>';
                    } else if(j == nowDate && i == nowMonth) {
                        tr += '<td class="nows"  full-date="'+theyear +"-"+themonth+"-"+ j + '" data-date="' + j + '"><div>今天</div></td>';
                    } else {
                        tr += '<td  full-date="'+theyear +"-"+themonth+"-"+ j +'" data-date="' + j + '"><div>' + j +'</div></td>';
                    };
                } else {
                    if(j < nowDate && i == nowMonth) {
                        tr += '<td class="over" full-date="' + theyear +"-"+themonth+"-"+ j +'" data-date="' + j + '"><div>' + j +'</div></td></tr><tr>';
                    } else if(j == nowDate && i == nowMonth) {
                        tr += '<td class="nows"  full-date="' +theyear +"-"+themonth+"-"+ j +  '" data-date="' + j + '"><div>今天</div></td></tr><tr>';
                    } else {
                        tr += '<td  full-date="' +theyear +"-"+themonth+"-"+ j + '" data-date="' + j + '"><div>' + j +'</div></td></tr><tr>';
                    }
                }
            }
            html += tr + '</tr></table>';
        }

        return html;

    },


	dayShow: function(year, month, date) { //时间段列表
		var html = '';
       var dataArr =  getSchedule1Date(getStandStrByParam(year, month, date))
		for (var i=0; i<dataArr.length; i++) {
       		var st = mobileDateSchedule.processContent(dataArr[i].fContentdesc).indexOf("余");
       		if(st == -1){
                html += '<div class="active sss" data-time="'+dataArr[i].fTimeStr+'">' +
                    '<span>'+dataArr[i].fTimeStr+'</span>' +
                    '<br>' +
                    '<span class="">'+mobileDateSchedule.processContent(dataArr[i].fContentdesc)+'</span>' +
                    '</div>';
			}else {
                html += '<div class="sss" data-time="'+dataArr[i].fTimeStr+'">' +
                    '<span>'+dataArr[i].fTimeStr+'</span>' +
                    '<br>' +
                    '<span class="">'+mobileDateSchedule.processContent(dataArr[i].fContentdesc)+'</span>' +
                    '</div>';
			}
		}
		return html
	},
    processContent: function(str) {
		var i= str.indexOf("余");
		if (i!= -1) {
			if (Number(str.substring(1,str.length)) > 0) {
				return str;
			} else {
				$(this).parent().addClass("active");
				return "售罄";
			}

		} else {
            $(this).parent().addClass("active");
			return str;
		}
	},
    monthcountShow: function() { //期限个数列表
        var html = '';
        //var dataArr =  getSchedule1Date(getStandStrByParam(year, month, date))
		var start = $("#leastTradNum").val();
        for (var i=start; i<= 36; i++) {
            html += '<div class="sss sbcplj" data-time=""><span>'+i+'个月</span></span></div>';
        }
        return html
    },

	getAllday: function(dateobj) {
		//计算当前月份有多少天
		var month = dateobj.getMonth() + 1;
		var year = dateobj.getFullYear();
		switch(month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				return 31
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				return 30
				break;
			case 2:
				if(year % 4 != 0) {
					return 28
				} else {
					return 29
				}
				break;
		}
	},
	getChinaDay: function(dayName) {
		switch(dayName) {
			case 1:
				return '一'
				break;
			case 2:
				return '二'
				break;
			case 3:
				return '三'
				break;
			case 4:
				return '四'
				break;
			case 5:
				return '五'
				break;
			case 6:
				return '六'
				break;
			case 7:
				return '七'
				break;
			case 8:
				return '八'
				break;
			case 9:
				return '九'
				break;
			case 10:
				return '十'
				break;
			case 11:
				return '十一'
				break;
			case 12:
				return '十二'
				break;
			default:
  				return "十二";
  				break;
		}
	},
	yearBack: function() {
		$('.data-wrap .sel-box,.month-box .show-list,.day-box .show-list').hide()
		$('.year-box .show-list').show()
	},
	monthBack: function() {
		$('.year-box .show-list,.month-box .sel-box,.day-box .show-list').hide()
		$('.year-box .sel-box,.month-box .show-list').show()
	},
    addDate: function(date, days) {
        if (days == undefined || days == '') {
            days = 0;
        }
        var date = new Date(date);
        date.setDate(date.getDate() + days);
        var month = date.getMonth() + 1;
        var day = date.getDate();
        return date.getFullYear() + '-' + mobileDateSchedule.getFormatDate(month) + '-' + mobileDateSchedule.getFormatDate(day);
	},
    getFormatDate: function(arg) {
        if (arg == undefined || arg == '') {
            return '';
        }

        var re = arg + '';
        if (re.length < 2) {
            re = '0' + re;
        }

        return re;
	},
   checkcompareTime2:  function (choseTime,beginTime) {
    var chose=new Date(choseTime.replace("-", "/").replace("-", "/"));
    var begin=new Date(beginTime.replace("-", "/").replace("-", "/"));
    if(chose<=begin){
        return true;
    } else {
        return false;
    }
}


}

//按时段排期
$.fn.serviceDateShow = function(opts) {
	var _this = this;
	this.defaults = {
		rangeMin: 20,
		rangeMax: 20
	};
	this.opts = $.extend({}, this.defaults, opts);
	var dateWrap = mobileDateSchedule.dateWrap("time");
	$('body').append(dateWrap);
	// 开始加载年份
	$('.data-wrap').show();
	// 年份点击加载日期
		$('.year-box .show-list').hide();
		$('.month-box .show-list').show();
		mobileDateSchedule.yearShow();
		//mobileDateSchedule.getYear();
	    //设置显示当前日期开始算7天内的排期
    	var endDate = mobileDateSchedule.addDate(new Date().Format("yyyy-MM-dd"), 28);
		$('.month-list').html(mobileDateSchedule.monthShowforSchedule1(mobileDateSchedule.selYear));
	    $('.month-list').find("[specildate]").each(function(){
            if (mobileDateSchedule.checkcompareTime2(endDate,$(this).attr("specildate"))) {
              	$(this).addClass("over");
            }
		});

    /**
	 * 初始显示日期
     * @type {Array}
     */
    if(opts != "请选择") {
        var dtime = opts.split(' ');
        var times = dtime[0].split('-');
        //$(".choice-time1").html(dtime[0].replace(mobileDateSchedule.selYear+"-", '').replace('-', '月')+'日'+'<i></i>');
        $(".choice-time1").html(times[1] + '月' + times[2] + '日' + '<i></i>');
        $(".choice-time3").html(dtime[1]);
        mobileDateSchedule.selMonth = times[1];
        mobileDateSchedule.selDay = times[2];
        mobileDateSchedule.selTime = dtime[1];

        // 为选择的日期添加标志
        var time = Number(times[0]) + "-" + Number(times[1]) + "-" +Number(times[2]);
        $(".month-box .month-list .now").removeClass("now");
        $(".month-box .month-list").find("td[specildate='"+time+"']").addClass("now");
    } else {
        $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');
    }

	//加载时间段
	$('.month-box').on('click', 'tbody td:not([class=over])', function() {
		if($(this).text() == ""){
			return;
		}
		$('.month-box .show-list').hide();
		$('.month-box .sel-box').show();
		$('.day-box .show-list').show();
		mobileDateSchedule.selDay = $(this).data('date');
		mobileDateSchedule.getDay("time");
        var arrDate = $(this).attr("specildate").split("-");
		mobileDateSchedule.selYear = arrDate[0];
		$('.day-list').html(mobileDateSchedule.dayShow(arrDate[0], arrDate[1], arrDate[2]));
        // 为选择的日期添加标志
        $(".month-box .month-list .now").removeClass("now");
        $(this).addClass("now");
		// 为选择的时间点添加标志
        var time = $(".choice-time3").text();
        // $('.day-list').find("div[data-time='" + time + "']").children().eq(0).addClass('active').append('<i></i>');
        $('.day-list').find("div[data-time='" + time + "']").addClass('select').append('<i></i>');
        $(".choice-time3").removeClass("disClick");
	});

	// 时间列表切换
	$(".choice-time3").on("click", function () {
		if($(this).hasClass("disClick")){
			return;
		}
        $('.month-box .show-list').hide();
        // $('.month-box .sel-box').show();
        $('.day-box .show-list').show();
        $(".choice-time3").append('<i></i>');
        $(".choice-time1").find('i').remove();
        $('.day-list').html(mobileDateSchedule.dayShow(mobileDateSchedule.selYear, mobileDateSchedule.selMonth, mobileDateSchedule.selDay));
        // 为选择的时间点添加标志
        var time = $(".choice-time3").text();
        // $('.day-list').find("div[data-time='" + time + "']").children().eq(0).addClass('active').append('<i></i>');
        $('.day-list').find("div[data-time='" + time + "']").addClass('select').append('<i></i>');
    })

    // 时间列表切换
    $(".choice-time1").on("click", function () {
        $('.month-box .show-list').show();
        // $('.month-box .sel-box').hide();
        $('.day-box .show-list').hide();
        $(".choice-time1").append('<i></i>');
        $(".choice-time3").find('i').remove();
    })

	// $('.day-box').on('click', 'span:not([class=over])', function() {
	// 	if($(this).parent().hasClass('active')) { // 过期不可点击
     //        return;
     //    }
	// 	if(!$(this).hasClass('active') && !$(this).siblings().hasClass('active')) {
	// 		mobileDateSchedule.selTime = $(this).closest("div").attr("data-time")
	// 		$(".choice-time3").html(mobileDateSchedule.selTime+'<i></i>')
	// 		$(this).addClass('active').append('<i></i>');
     //        $(this).closest("div").siblings().find('span').removeClass('active').find('i').remove()
    //
     //        $('.isEnterXm').removeClass('isEnterXm').addClass('date-confirm');//确认显示
	// 	} else {
     //        $(this).removeClass('active').find('i').remove();
     //        $(this).siblings().removeClass('active').find('i').remove();
     //        $(".choice-time3").html('')
    //
     //        $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');//确认灰掉
	// 	}
	// })

    $('.day-box').on('click', 'div.sss', function() {
        if($(this).hasClass('active')) { // 过期不可点击
            return;
        }
        if(!$(this).hasClass('select')) {
            mobileDateSchedule.selTime = $(this).attr("data-time");
            $(".choice-time3").html(mobileDateSchedule.selTime+'<i></i>');
            $(this).addClass('select').append('<i></i>');
            $(this).siblings().removeClass('select').find('i').remove();
            $('.isEnterXm').removeClass('isEnterXm').addClass('date-confirm');//确认显示
        } else {
            $(this).removeClass('select').find('i').remove();
            $(this).siblings().removeClass('select').find('i').remove();
            $(".choice-time3").html('');
            $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');//确认灰掉
        }
    });

    $(document).on("click", ".date-confirm", function () {
        if($(this).attr('class') == 'isEnterXm') {
            return ;
        }

		var dateBox = mobileDateSchedule.selYear + '-' + mobileDateSchedule.selMonth + '-' + mobileDateSchedule.selDay + ' ' + mobileDateSchedule.selTime
		$("body").find(".selectSchedule").text(dateBox);
		//待确认订单页修改调用更改隐藏域的值
        if ($("#currpage").length > 0 && $("#currpage").val() =="orderpage") {
				var datajson = JSON.parse($("#fDatas").text());
				var orderTimeObj = {};
            	var dataArr = dateBox.split(" ");
            	orderTimeObj.time1= dataArr[0];
            	orderTimeObj.time2= dataArr[1];
				datajson.f_order_time= JSON.stringify(orderTimeObj);
            $("#fDatas").text(JSON.stringify(datajson))
        }
		$('.data-wrap').remove()
	});

	$(".data-wrap .cont").click(function(){
        $('.data-wrap').remove()
	});

    //按时段排期的清空选择
    $(document).on("click",".removeAllSelect",function () {
        $(".choice-time1").html("选择日期<i></i>");
		$(".choice-time3").html("选择时段").addClass("disClick");

        $('.year-box .show-list').hide();
        $('.month-box .show-list').show();
        mobileDateSchedule.yearShow();
        //mobileDateSchedule.getYear();
        //设置显示当前日期开始算7天内的排期
        var endDate = mobileDateSchedule.addDate(new Date().Format("yyyy-MM-dd"), 14);
        $('.month-list').html(mobileDateSchedule.monthShowforSchedule1(mobileDateSchedule.selYear));
        $('.month-list').find("[specildate]").each(function(){
            if (mobileDateSchedule.checkcompareTime2(endDate,$(this).attr("specildate"))) {
                $(this).addClass("over")
            }
        });
        $('.day-list').html("");
        $('.day-box .show-list').hide();
        $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');
        $(this).css({"opacity":.2}).prop("disabled",true);
    })
};

//按天排期
$.fn.serviceDateShow1 = function(opts) {
    var _this = this;
    this.defaults = {
        rangeMin: 20,
        rangeMax: 20
    };
    this.opts = $.extend({}, this.defaults, opts);
    var dateWrap = mobileDateSchedule.dateWrap("day");
    $('body').append(dateWrap);
    // 开始加载年份
    $('.data-wrap').show();

    // 年份点击加载日期
    $('.year-box .show-list').hide();
    $('.month-box .show-list').show();
    mobileDateSchedule.yearShow();
    //设置显示当前日期开始算180天内的排期
    var endDate = mobileDateSchedule.addDate(new Date().Format("yyyy-MM-dd"), 180);

    $('.month-list').html(mobileDateSchedule.monthShowforSchedule2(mobileDateSchedule.selYear));
    $('.month-list').find("[full-date]").each(function(){
        var arrDate = $(this).attr("full-date").split("*");
        if (mobileDateSchedule.checkcompareTime2(endDate,arrDate[0]+"-"+arrDate[1] + "-" +arrDate[2])) {
            $(this).addClass("over")
        }
    });
    var getDateArr = getSchedule2Date();
    for (var i=0; i<getDateArr.length; i++) {
    	var  objDate =  getDateArr[i];
        var dates = objDate["fMonthHead"] + "*" + objDate["fDay"];
        dates =  dates.replaceAll("-", "*");
		if (!$("[full-date='"+dates+"']").hasClass("over") && objDate.fStock - objDate.fOrderNum <=0) {
			$("[full-date='"+dates+"']").html( $("[full-date='"+dates+"']").attr("data-date")+'<div class="isnull">满</div>').addClass("isfull");
		}
    }


    /**
	 * 初始显示日期
     * @type {Array}
     */
    if(opts != "请选择>" && opts != "请选择") {
        var dtime = opts.split('>');
        var a1 = dtime[0].split('-')
        var a2 = dtime[1].split('-')
        var time1 = a1[1] + '月' + a1[2]; //dtime[0].replace(mobileDateSchedule.selYear + "-", '').replace('-', '月');
        var time2 = a2[1] + '月' + a2[2]; //dtime[1].replace(mobileDateSchedule.selYear + "-", '').replace('-', '月');
        $(".choice-time1").attr('dateattr',dtime[0]).html(time1+"日"+'<i></i>');
        $(".choice-time3").attr('dateattr',dtime[1]).html(time2+"日");
        mobileDateSchedule.selData1 = dtime[0];
        mobileDateSchedule.selData2 = dtime[1];
        var time = a1[0] + "*" + a1[1] + "*" + Number(a1[2]);
        $(".month-box .month-list .now").removeClass("now");
        $(".month-box .month-list").find("td[full-date='"+time+"']").addClass("now");
    } else {
        $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');
    }

    //加载时间段
	var count = 0;
    $('.month-box').on('click', 'tbody td:not([class=over])', function() {
        if($(this).text() == ""){
            return;
        }
        //mobileDateSchedule.selDay = $(this).data('date')
		if ($(this).hasClass("over")/* && $(this).hasClass("nows")*/) {
			return;
		}
		if ($(this).hasClass("isfull") /*&& $(".choice-time").find("i").closest("div").attr("class") =="choice-time1"*/) {
			return;
		}
		var fullDate = $(this).attr("full-date");
		mobileDateSchedule.getDay("day", fullDate);
        $(".month-box .month-list .now").removeClass("now");
        if(count==0) {
            $(".choice-time3").attr('dateattr','').html('结束日期'+'<i></i>');
            $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');
        } else {
        	if($(".choice-time3").attr("dateattr") == "" ) {
                $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');
            } else {
                $('.isEnterXm').removeClass('isEnterXm').addClass('date-confirm');
                $(this).addClass("now");
			}
        }
        count++;
        $(".choice-time3").removeClass("disClick");
    });

    /*$('.day-box').on('click', 'span:not([class=over])', function() {
        if(!$(this).hasClass('active')) {
            mobileDateSchedule.selTime = $(this).closest("div").attr("data-time")
            $(".choice-time3").html(mobileDateSchedule.selTime+'<i></i>')
            $(this).addClass('active').append('<i></i>').siblings().removeClass('active').find('i').remove()
        }
    })*/
    
    $(".choice-time3").on("click", function () {
        if($(this).hasClass("disClick")){
            return;
        }
        var fullDate = $(this).closest(".choice-time").find(".choice-time1").attr("dateattr").split('-').join("*");
        mobileDateSchedule.getDay("day", fullDate, true);
        $(".month-box .month-list .now").removeClass("now");
        // var fullDate = dtime[0].replaceAll("-", "*");
        // mobileDateSchedule.getDay("day", fullDate);
        $(".choice-time3").append("<i></i>");
        $(".choice-time1").find('i').remove();
        var a3 = $(this).attr("dateattr").split('-');
        var time = a3[0] + "*" + a3[1] + "*" + Number(a3[2]);
        $(".month-box .month-list .now").removeClass("now");
        $(".month-box .month-list").find("td[full-date='"+time+"']").addClass("now");
        count = 1;
        if($(".removeAllSelect").prop("disabled") == true){
            $("[full-date]").removeClass("now");
        }
    })

    $(".choice-time1").on("click", function () {
        $(".month-box .month-list .over").removeClass("over");
        $(".choice-time1").append("<i></i>");
        $(".choice-time3").find('i').remove();
        var date = new Date().getTime() - (24 * 3600 * 1000);
        setOverDateChoseWhenStarChose(new Date(date).format("yyyy-MM-dd"));
        var a4 = $(this).attr("dateattr").split('-');
        var time = a4[0] + "*" + a4[1] + "*" + Number(a4[2]);
        $(".month-box .month-list .now").removeClass("now");
        $(".month-box .month-list").find("td[full-date='"+time+"']").addClass("now");
        count = 0;
        if($(".removeAllSelect").prop("disabled") == true){
            $("[full-date]").removeClass("now");
        }
    })

    $(document).on("click", ".date-confirm", function () {
    	if($(this).attr('class') == 'isEnterXm') {
			return ;
        }
        if ($("#currpage").length > 0 && $("#currpage").val() =="orderpage") {

        	//$(".selectSchedule").text($(".choice-time1").attr("dateattr")+"(中午)-"+$(".choice-time3").attr("dateattr")+"(中午)")
        	$(".selectSchedule").text(mobileDateSchedule.selData1+"(中午)-"+mobileDateSchedule.selData2+"(中午)")
        	//更改隐藏域的值
            var datajson = JSON.parse($("#fDatas").text());
            var orderTimeObj = {};
            orderTimeObj.time1= mobileDateSchedule.selData1;
            orderTimeObj.time2= mobileDateSchedule.selData2;
            var musDay = DateDiff( orderTimeObj.time1,  orderTimeObj.time2)
            datajson.f_order_time= JSON.stringify(orderTimeObj);

			//价格计算待续
            if (datajson.f_pay_type == 0)  {
                var xxxx = Number(datajson.f_orderNum_total_price) * musDay.toFixed(2);
                $(".fTotalPrice").text("￥"+xxxx);
                datajson.f_total_price=xxxx;
                datajson.f_curr_price=xxxx;

            } else if (datajson.f_pay_type == 1) {
                var totaldeposit = (JSON.parse(datajson.f_pay_content).f_deposit)*datajson.f_order_num;
                var xxxx = Number(datajson.f_orderNum_total_price) * musDay+ totaldeposit;
                datajson.f_total_price=xxxx;

			}
            $("#fDatas").text(JSON.stringify(datajson))
        } else {
            var scheStr = '<div class="bar bb-f2 bt-f2 bg-4"><div class="bar-show padding-buy">'+
                '  <span class="">服务耗时：</span>'+
                '  <p class="fr">'+
                '    <span class="fTake">1</span>'+
                '    <span class="fTakeType">天</span></p>'+
                '</div></div>'+
                '<div class="bar bb-f2 arrow-r bg-4"><div class="bar-show padding-buy">'+
                '  <span class="">服务时间：</span>'+
                '  <p class="fr">'+
                // '    <span class="schedul1">'+$(".choice-time1").attr("dateattr")+'</span>(中午)-<span class="schedul2">'+$(".choice-time3").attr("dateattr")+'</span>(中午)</p>'+
                '    <span class="schedul1">'+mobileDateSchedule.selData1+'</span>(中午)-<span class="schedul2">'+mobileDateSchedule.selData2+'</span>(中午)</p>'+
                '</div></div>';
            _this.html(scheStr);
		}
        $('.data-wrap').remove()
    });

    $(".data-wrap .cont").click(function(){
        $('.data-wrap').remove()
    });

    //按天排期的清空选择
    $(document).on("click",".removeAllSelect",function () {
        $(".choice-time1").html("开始日期<i></i>");
        $(".choice-time3").html("结束日期").addClass("disClick");
		var choseEnd = $(".choice-time1").attr("dateattr");
        $('.month-list').find("[full-date]").each(function(){
            $(this).removeClass("now");
            var arrDate = $(this).attr("full-date").split("*");
          if (mobileDateSchedule.checkcompareTime2(arrDate[0]+"-"+arrDate[1] + "-" +arrDate[2], choseEnd)
			  && mobileDateSchedule.checkcompareTime2(new Date().Format("yyyy-MM-dd"),arrDate[0]+"-"+arrDate[1] + "-" +arrDate[2])) {
              $(this).removeClass("over")
		  }
        });
        $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');
        $(this).css({"opacity":.2}).prop("disabled",true);
    })

};

//按月排期
$.fn.serviceDateShow2 = function(opts) {
    var _this = this;
    this.defaults = {
        rangeMin: 20,
        rangeMax: 20
    }
    this.opts = $.extend({}, this.defaults, opts);

    var dateWrap = mobileDateSchedule.dateWrap("month");
    $('body').append(dateWrap)
    // 开始加载年份
    $('.data-wrap').show()

    // 年份点击加载日期
    $('.year-box .show-list').hide()
    $('.month-box .show-list').show()
    mobileDateSchedule.yearShow()
    //mobileDateSchedule.getYear()
    $('.month-list').html(mobileDateSchedule.monthShowforSchedule3(mobileDateSchedule.selYear))
    // })
    setSchedule3Content(getSchedule3Date())


    /**
	 * 初始显示日期
     * @type {Array}
     */
    if(opts != "请选择>" && opts != "请选择") {
        var dtime = opts.split('>');
        var str = dtime[0].split("-");
        //$(".choice-time1").attr('dateattr', dtime[0]).html(dtime[0].replace(mobileDateSchedule.selYear+"-", '').replace('-', '月')+'日'+'<i></i>');
        $(".choice-time1").attr('dateattr', dtime[0]).html(str[1] + '月' + str[2] + '日' + '<i></i>');
        $(".choice-time3").html(dtime[1]);
        mobileDateSchedule.selTime = dtime[1];
        mobileDateSchedule.selData = dtime[0];
        var time = Number(str[0]) + "-" + Number(str[1]) + "-" + Number(str[2]);
        $(".month-box .month-list .now").removeClass("now");
        $(".month-box .month-list").find("td[full-date='"+time+"']").addClass("now");
    } else {
        $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');
	}

    //加载期限
    $('.month-box').on('click', 'tbody td:not([class=over])', function() {
        if($(this).text() == ""){
            return;
        }
        $('.month-box .show-list').hide();
        $('.month-box .sel-box').show();
        $('.day-box .show-list').show();
        mobileDateSchedule.selDay = $(this).data('date');
        mobileDateSchedule.getDay("month",$(this).attr("full-date"));
        mobileDateSchedule.selData = $(this).attr("full-date");
        $('.day-list').html(mobileDateSchedule.monthcountShow());

        $(".month-box .month-list .now").removeClass("now");
		$(this).addClass("now")
        var str = $(".choice-time3").text().trim();
        // $('.day-list').find("span").each(function () {
        //     if($(this).text() == str) {
        //         $(this).append('<i></i>').addClass("active");
        //     }
        // });
        $('.day-list').find(".sss").each(function () {
			if($(this).find("span").text() == str) {
                $(this).addClass("select").append('<i></i>');
            }
        });
        $(".choice-time3").removeClass("disClick");
    });

	// 列表切换
	$(".choice-time3").on("click", function () {
        if($(this).hasClass("disClick")){
            return;
        }
        $('.month-box .show-list').hide();
        $('.month-box .sel-box').show();
        $('.day-box .show-list').show();
		$(this).append('<i></i>');
        $(".choice-time1").find('i').remove();
        $('.day-list').html(mobileDateSchedule.monthcountShow());
        var str = $(".choice-time3").text().trim();
        $('.day-list').find(".sss").each(function () {
            if($(this).find("span").text() == str) {
                $(this).addClass("select").append('<i></i>');
            }
        });
        // $('.day-list').find("span").each(function () {
        //     if($(this).text() == str) {
        //         $(this).append('<i></i>').addClass("active");
        //     }
        // });
    });

    // 列表切换
    $(".choice-time1").on("click", function () {
        $('.month-box .show-list').show();
        // $('.month-box .sel-box').hide();
        $('.day-box .show-list').hide();
        $(this).append('<i></i>');
        $(".choice-time3").find('i').remove();
    })

    $('.day-box').on('click', 'div.sss', function() {
        if(!$(this).hasClass('select')) {
            mobileDateSchedule.selTime = $(this).find("span").text();
            $(".choice-time3").html(mobileDateSchedule.selTime+'<i></i>')
            $(this).addClass('select').append('<i></i>');
            $(this).siblings().removeClass("select").find('i').remove();
            $('.isEnterXm').removeClass('isEnterXm').addClass('date-confirm');//确认显示
        } else {
        	$(this).removeClass('select').find('i').remove();
            $(".choice-time3").html('');

            $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');// 确认灰掉
		}
    })
    //
    // $('.day-box').on('click', 'div.sss', function() {
    //     if($(this).hasClass('active')) { // 过期不可点击
    //         return;
    //     }
    //     if(!$(this).hasClass('select')) {
    //         mobileDateSchedule.selTime = $(this).attr("data-time")
    //         $(".choice-time3").html(mobileDateSchedule.selTime+'<i></i>');
    //         $(this).addClass('select').append('<i></i>');
    //         $(this).siblings().removeClass('select').find('i').remove();
    //         $('.isEnterXm').removeClass('isEnterXm').addClass('date-confirm');//确认显示
    //     } else {
    //         $(this).removeClass('select').find('i').remove();
    //         $(this).siblings().removeClass('active').find('i').remove();
    //         $(".choice-time3").html('');
    //         $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');//确认灰掉
    //     }
    // });

    $(document).on("click", ".date-confirm", function () {
        if($(this).attr('class') == 'isEnterXm') {
            return ;
        }


        //var dateStr = $(".choice-time1").attr("dateattr");
        var dateStr = mobileDateSchedule.selData;
        if ($("#currpage").length > 0 && $("#currpage").val() =="orderpage") {

            $(".selectSchedule").text(dateStr+"(下午)开始"+mobileDateSchedule.selTime)
            //更改隐藏域的值
            var datajson = JSON.parse($("#fDatas").text());
            var orderTimeObj = {};
            orderTimeObj.time1= dateStr;
            orderTimeObj.time2= mobileDateSchedule.selTime;
            datajson.f_order_time= JSON.stringify(orderTimeObj);
			var musDay = orderTimeObj.time2.substring(0,orderTimeObj.time2.indexOf("个"))
            //价格计算待续
            if (datajson.f_pay_type == 0)  {
                var xxxx = Number((datajson.f_orderNum_total_price) * musDay).toFixed(2);
                $(".fTotalPrice").text("￥"+xxxx);
                datajson.f_total_price=xxxx;
                datajson.f_curr_price=xxxx;

            } else if (datajson.f_pay_type == 1) {
                var totaldeposit = (JSON.parse(datajson.f_pay_content).f_deposit)*datajson.f_order_num;
                var xxxx = Number(datajson.f_orderNum_total_price) * musDay+ totaldeposit;
                datajson.f_total_price=xxxx;

            }
            $("#fDatas").text(JSON.stringify(datajson))
        } else {

            var scheStr = '<div class="bar bb-f2 bt-f2 bg-4"><div class="bar-show padding-buy">'+
                '  <span class="">服务耗时：</span>'+
                '  <p class="fr">'+
                '    <span class="fTake">1</span>'+
                '    <span class="fTakeType">个月</span></p>'+
                '</div></div>'+
                '<div class="bar bb-f2 arrow-r bg-4"><div class="bar-show padding-buy">'+
                '  <span class="">服务时间：</span>'+
                '  <p class="fr">'+
                '    <span class="schedul1">'+mobileDateSchedule.selData+'</span> (下午)<span class="schedul2 margin-l5">开始 '+mobileDateSchedule.selTime+'</span></p>'+
                '</div></div>';
            _this.html(scheStr);
		}
        $('.data-wrap').remove();
    });
    $(".data-wrap .cont").click(function(){
        $('.data-wrap').remove()
    });

    //按月排期的清空选择
    $(document).on("click",".removeAllSelect",function () {
        $(".choice-time1").html("开始日期<i></i>");
        $(".choice-time3").html("期限").addClass("disClick");
        // 年份点击加载日期
        $('.year-box .show-list').hide();
        $('.month-box .show-list').show();
        mobileDateSchedule.yearShow();
        //mobileDateSchedule.getYear();
        $('.month-list').html(mobileDateSchedule.monthShowforSchedule3(mobileDateSchedule.selYear))
        // })
        setSchedule3Content(getSchedule3Date());
        $('.day-list').html("");
        $('.day-box .show-list').hide();
        $('.date-confirm').removeClass('date-confirm').addClass('isEnterXm');
        $(this).css({"opacity":.2}).prop("disabled",true);
    });
};



//按时段排期
function getSchedule1Date(strDate) {
    var timeperiod = ""
    ajaxUtil.ajaxCommonUtil({
        url:zumaURL+"/wap/service_wap/zmNoteWapService/queryServiceScheduleDataByTime",
        type:"get",
        async: false,
        /*xhrFields: { withCredentials: true },*/
        data :{"fId":$("#fChildId").val(), "strDate":strDate,"fShopId":$("#fShopId").val()},
        dataType:"json",
        success:function(result){
            if (result.status == 0) {
                //setSchedule1Content(result.data)
                timeperiod = result.data;
            } else {
                alert(result.message);
            }
        },error:function(){
            alert("系统繁忙")
        }
    });
    return timeperiod;
}


//按天排期
function getSchedule2Date(strDate) {
    var timeperiod = ""
    ajaxUtil.ajaxCommonUtil({
        url:zumaURL+"/wap/service_wap/zmNoteWapService/queryServiceScheduleDataByDay2",
        type:"get",
        async: false,
        /*xhrFields: { withCredentials: true },*/
        data :{"fId":$("#fChildId").val(),"fShopId":$("#fShopId").val()},
        dataType:"json",
        success:function(result){
            if (result.status == 0) {
                //setSchedule1Content(result.data)
                timeperiod = result.data;
            } else {
                alert(result.message);
            }
        },error:function(){
            alert("系统繁忙")
        }
    });
    return timeperiod;
}


//按月排期
function getSchedule3Date() {
    var monthperiod = ""
    ajaxUtil.ajaxCommonUtil({
        url:zumaURL+"/wap/service_wap/zmNoteWapService/queryServiceScheduleDataByMonth",
        type:"get",
        async: false,
        /*xhrFields: { withCredentials: true },*/
        data :{"fId":$("#fChildId").val(),"fShopId":$("#fShopId").val()},
        dataType:"json",
        success:function(result){
            if (result.status == 0) {
                console.info(result);
                monthperiod = result.data

            } else {
                alert(result.message);
            }
        },error:function(){
            alert("系统繁忙")
        }
    });
    return monthperiod;
}


//当选择后开始日期后，根据当前最少交易量来控制当前结束日期可选择的范围
function  setOverDateChoseWhenStarChose(startDateStr) {
	console.info(startDateStr)

	var maxDate = mobileDateSchedule.addDate(startDateStr, Number($("#leastTradNum").val()) - 1);
	console.info(maxDate)

	/*if (startDateStr == maxDate) {
		$(".now").addClass("over")
	}*/
    $('.month-list').find("[full-date]").each(function(){
        var arrDate = $(this).attr("full-date").split("*");
        if (mobileDateSchedule.checkcompareTime2(arrDate[0]+"-"+arrDate[1] + "-" +arrDate[2], maxDate)) {
            $(this).addClass("over")
        }

    })

}

//设置按月排期的经营可选时间
function setSchedule3Content(data) {
    var json = JSON.parse(data);
    var map = new Map();
    map.arr = json.arr;

    var beginDate = map.get("beginDate");
    var maxMonth = Number(map.get("monthNum"));
    var endDate = mobileDateSchedule.addDate(beginDate, maxMonth*30)

    $("[full-date]").each(function(){
        if (checkcompareTime($(this).attr("full-date"),map.get("beginDate"))) {
            $(this).addClass("over")
        }
        if (checkcompareTime(endDate, $(this).attr("full-date"))) {
            $(this).addClass("over")
        }
    })
}




//计算天数差的函数，通用
function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2002-12-18格式
    var  aDate,  oDate1,  oDate2,  iDays
    aDate  =  sDate1.split("-")
    oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2002格式
    aDate  =  sDate2.split("-")
    oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])
    iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数
    return  iDays
}

/*
$(".removeAllSelect").click(function () {
	alert("aaaa")
})*/
